package com.atguigu.redisconfig;

import com.atguigu.constant.RedisConst;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
public class RedisChannelConfig {

    //用哪个类哪个方法处理接收到的消息
    @Bean
    MessageListenerAdapter messageListenerAdapter(ShopMessageListener shopMessageListener) {
        return new MessageListenerAdapter(shopMessageListener,"receiveChannelMessage");
    }

    /**
     * 你订阅哪个频道 已经连接的redis是谁 以及用哪个类处理消息
     * @param factory redis的连接工厂
     * @param messageListenerAdapter 当有消息来的时候接收并处理的一个消息类
     * @return
     */
    @Bean
    RedisMessageListenerContainer container(RedisConnectionFactory factory, MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(factory);
        //订阅哪个频道
        listenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic(RedisConst.PREPARE_PUB_SUB_SECKILL));
        return listenerContainer;
    }
}
